function formatLeadingZeroNumber(n, digitNum = 2) {
  n = n.toString()
  const needNum = Math.max(digitNum - n.length, 0)
  return new Array(needNum).fill(0).join('') + n
}

//输出格式为：2019-10-30 14:25:55
function formatDateTime(date, withMs = false) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  const ms = date.getMilliseconds()

  let ret = [year, month, day].map(value => formatLeadingZeroNumber(value, 2)).join('-') +
    ' ' + [hour, minute, second].map(value => formatLeadingZeroNumber(value, 2)).join(':')
  if (withMs) {
    ret += '.' + formatLeadingZeroNumber(ms, 3)
  }
  return ret
}
//输出当前时间的字符串，格式为：2019/12/12 12:12
function getDateTime() {
  var date_time = formatDateTime(new Date());
  date_time = date_time.slice(0, date_time.lastIndexOf(":")).replace(/-/g, "/")
  return date_time
}

//输出当前时间的字符串，格式为：12/12 12:12
function getDateTimeShort() {
  var date_time = formatDateTime(new Date());
  date_time = date_time.slice(date_time.indexOf("-") + 1, date_time.lastIndexOf(":")).replace("-", "/")
  return date_time
}

//判断和当前时间的大小
function compareToCurTime(mtime) {
  var thetime = '2020-03-19 19:09:00';
  var date = new Date(Date.parse(mtime.replace(/-/g, "/")));
  var curDate = new Date();
  if (date <= curDate) {
    return -1
  } else {
    return 1
  }
}

module.exports = {
  getDateTime,
  compareToCurTime
}